<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="港口名称" prop="port">
        <el-input
          v-model="queryParams.port"
          placeholder="请输入港口名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货主名称" prop="owner">
        <el-input
          v-model="queryParams.owner"
          placeholder="请输入货主名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="合同类型" prop="contractType">
        <el-select v-model="queryParams.contractType" placeholder="请选择合同类型" clearable>
          <el-option
            v-for="dict in dict.type.sys_contract_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="签订日期" prop="signDate">
        <el-date-picker clearable
          v-model="queryParams.signDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择签订日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="生效日期" prop="effectDate">
        <el-date-picker clearable
          v-model="queryParams.effectDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择合同生效日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="终止日期" prop="expirationDate">
        <el-date-picker clearable
          v-model="queryParams.expirationDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择合同终止日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="合同状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择合同状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_contract_cstatus"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="到验号" prop="uniqueCode">
        <el-input
          v-model="queryParams.uniqueCode"
          placeholder="请输入到验号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="船名" prop="shipName">
        <el-input
          v-model="queryParams.shipName"
          placeholder="请输入船名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['BussinessContract:contractmanadge:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['BussinessContract:contractmanadge:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['BussinessContract:contractmanadge:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['BussinessContract:contractmanadge:export']"
        >导出</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="el-icon-s-check"
          size="mini"
          :disabled="multiple"
          @click="handleSubmitReview"
          v-hasPermi="['BussinessContract:contractmanadge:review']"
        >合同送审</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-tabs v-model="currentStatus" @tab-click="handleTabClick">
      <el-tab-pane label="草案" name="草案"></el-tab-pane>
      <el-tab-pane label="审核中" name="审核中"></el-tab-pane>
      <el-tab-pane label="审核退回" name="审核退回"></el-tab-pane>
      <el-tab-pane label="审核通过" name="审核通过"></el-tab-pane>
    </el-tabs>

    <el-table v-loading="loading" :data="contractmanadgeList" @selection-change="handleSelectionChange"  @row-click="handleRowClick">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="合同号" align="center" prop="contractId" />
      <el-table-column label="港口" align="center" prop="port">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_port_owner2" :value="scope.row.port"/>
        </template>
      </el-table-column>
      <el-table-column label="货主" align="center" prop="owner">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_port_owner" :value="scope.row.owner"/>
        </template>
      </el-table-column>
      <el-table-column label="合同类型" align="center" prop="contractType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_contract_type" :value="scope.row.contractType"/>
        </template>
      </el-table-column>
        <el-table-column label="货类" align="center" prop="cargoType" />
      <el-table-column label="合同签订日期" align="center" prop="signDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.signDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同生效日期" align="center" prop="effectDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.effectDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同终止日期" align="center" prop="expirationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.expirationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_contract_cstatus" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="合同内容摘要" align="center" prop="contentSummary" />
      <el-table-column label="合同内容哈希值" align="center" prop="contentHash" />
      <el-table-column label="到验号" align="center" prop="uniqueCode" />
      <el-table-column label="船名" align="center" prop="shipName" />
      <el-table-column label="作废日期" align="center" prop="abolishDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.abolishDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="公司ID" align="center" prop="companyId" />
      <el-table-column label="货物类型" align="center" prop="cargoType" />
      <el-table-column label="合同总量" align="center" prop="volume" />
      <el-table-column label="单位" align="center" prop="unit" />
      <el-table-column label="单价" align="center" prop="price" />
      <el-table-column label="币种" align="center" prop="currency" />
      <el-table-column label="备注说明" align="center" prop="remarks" />
<!--      <el-table-column label="合同原件" align="center" prop="contentUrl" />-->
      <el-table-column
        label="合同原件"
        align="center"
        prop="contentUrl">
        <template v-slot="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleDownload(scope.row)"
          >下载</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['BussinessContract:contractmanadge:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['BussinessContract:contractmanadge:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 审核记录列表 -->
    <el-card class="box-card" v-if="selectedContractId">
      <div slot="header" class="clearfix">
        <span>审核记录</span>
      </div>
      <el-table v-loading="auditLoading" :data="auditList" style="width: 100%">
        <el-table-column prop="recordId" label="审核记录ID" />
        <el-table-column prop="portApprovalTime" label="港口审批时间" />
        <el-table-column prop="ownerApprovalTime" label="货主审批时间" />
        <el-table-column prop="portApprovalStatus" label="港口审批状态" />
        <el-table-column prop="ownerApprovalStatus" label="货主审批状态" />
        <el-table-column prop="portApprovalComments" label="港口审批意见" />
        <el-table-column prop="ownerApprovalComments" label="货主审批意见" />
      </el-table>
    </el-card>

    <!-- 添加BussinessContract对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同号" prop="port">
          <el-input v-model="form.contractId" placeholder="请输入合同号" />
        </el-form-item>
        <el-form-item label="港口名" prop="port">
<!--          <el-input v-model="form.port" placeholder="请输入港口名称或编号" />-->
              <el-select v-model="form.port" placeholder="请选择港口名称">
                <el-option
                  v-for="dict in dict.type.sys_port_owner2"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
        </el-form-item>
<!--        <el-form-item label="货主名称或ID" prop="owner">-->
<!--          <el-input v-model="form.owner" placeholder="请输入货主名称或ID" />-->
<!--        </el-form-item>-->
        <el-form-item label="货主" prop="contractType">
          <el-select v-model="form.owner" placeholder="请选择货主名称">
            <el-option
              v-for="dict in dict.type.sys_port_owner"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型">
            <el-option
              v-for="dict in dict.type.sys_contract_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同签订日期" prop="signDate">
          <el-date-picker clearable
            v-model="form.signDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择合同签订日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同生效日期" prop="effectDate">
          <el-date-picker clearable
            v-model="form.effectDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择合同生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同终止日期" prop="expirationDate">
          <el-date-picker clearable
            v-model="form.expirationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择合同终止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_contract_cstatus"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合同内容摘要" prop="contentSummary">
          <el-input v-model="form.contentSummary" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="合同全文存储链接" prop="contentUrl">
          <file-upload v-model="form.contentUrl"/>
        </el-form-item>
        <el-form-item label="合同内容哈希值" prop="contentHash">
          <el-input v-model="form.contentHash" placeholder="请输入合同内容哈希值" />
        </el-form-item>
        <el-form-item label="到验号" prop="uniqueCode">
          <el-input v-model="form.uniqueCode" placeholder="请输入到验号" />
        </el-form-item>
        <el-form-item label="船名" prop="shipName">
          <el-input v-model="form.shipName" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="货类" prop="shipName">
          <el-input v-model="form.cargoType" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="作废日期" prop="abolishDate">
          <el-date-picker clearable
            v-model="form.abolishDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择作废日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="公司ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司ID" />
        </el-form-item>
        <el-form-item label="合同总量" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入合同总量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="币种" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入币种" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 修改BussinessContract对话框 -->
    <el-dialog :title="title" :visible.sync="openUpdate" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同号" prop="port" disabled="true">
          <el-input v-model="form.contractId" placeholder="请输入港口名称或编号" />
        </el-form-item>
        <el-form-item label="港口名" prop="port">
          <!--          <el-input v-model="form.port" placeholder="请输入港口名称或编号" />-->
          <el-select v-model="form.port" placeholder="请选择港口名称">
            <el-option
              v-for="dict in dict.type.sys_port_owner2"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="货主名称或ID" prop="owner">-->
        <!--          <el-input v-model="form.owner" placeholder="请输入货主名称或ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="货主" prop="owner">
          <el-select v-model="form.owner" placeholder="请选择货主名称">
            <el-option
              v-for="dict in dict.type.sys_port_owner"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型">
            <el-option
              v-for="dict in dict.type.sys_contract_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同签订日期" prop="signDate">
          <el-date-picker clearable
                          v-model="form.signDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同签订日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同生效日期" prop="effectDate">
          <el-date-picker clearable
                          v-model="form.effectDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同终止日期" prop="expirationDate">
          <el-date-picker clearable
                          v-model="form.expirationDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同终止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_contract_cstatus"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合同内容摘要" prop="contentSummary">
          <el-input v-model="form.contentSummary" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="合同全文存储链接" prop="contentUrl">
          <file-upload v-model="form.contentUrl"/>
        </el-form-item>
        <el-form-item label="合同内容哈希值" prop="contentHash">
          <el-input v-model="form.contentHash" placeholder="请输入合同内容哈希值" />
        </el-form-item>
        <el-form-item label="到验号" prop="uniqueCode">
          <el-input v-model="form.uniqueCode" placeholder="请输入到验号" />
        </el-form-item>
        <el-form-item label="船名" prop="shipName">
          <el-input v-model="form.shipName" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="货类" prop="shipName">
          <el-input v-model="form.cargoType" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="作废日期" prop="abolishDate">
          <el-date-picker clearable
                          v-model="form.abolishDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择作废日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="公司ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司ID" />
        </el-form-item>
        <el-form-item label="合同总量" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入合同总量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="币种" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入币种" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFormUpdate">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 合同送审BussinessContract对话框 -->
    <el-dialog :title="'合同送审确认 - ' + form.contractId" :visible.sync="openReview" width="500px" append-to-body>
      <el-form ref="Form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同号" prop="port" disabled="true">
          <el-input v-model="form.contractId" placeholder="请输入港口名称或编号" />
        </el-form-item>
        <el-form-item label="港口名" prop="port">
          <!--          <el-input v-model="form.port" placeholder="请输入港口名称或编号" />-->
          <el-select v-model="form.port" placeholder="请选择港口名称">
            <el-option
              v-for="dict in dict.type.sys_port_owner2"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <!--        <el-form-item label="货主名称或ID" prop="owner">-->
        <!--          <el-input v-model="form.owner" placeholder="请输入货主名称或ID" />-->
        <!--        </el-form-item>-->
        <el-form-item label="货主" prop="owner">
          <el-select v-model="form.owner" placeholder="请选择货主名称">
            <el-option
              v-for="dict in dict.type.sys_port_owner"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同类型" prop="contractType">
          <el-select v-model="form.contractType" placeholder="请选择合同类型">
            <el-option
              v-for="dict in dict.type.sys_contract_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="合同签订日期" prop="signDate">
          <el-date-picker clearable
                          v-model="form.signDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同签订日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同生效日期" prop="effectDate">
          <el-date-picker clearable
                          v-model="form.effectDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同生效日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同终止日期" prop="expirationDate">
          <el-date-picker clearable
                          v-model="form.expirationDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择合同终止日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="合同状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_contract_cstatus"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="合同内容摘要" prop="contentSummary">
          <el-input v-model="form.contentSummary" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="合同全文存储链接" prop="contentUrl">
          <file-upload v-model="form.contentUrl"/>
        </el-form-item>
        <el-form-item label="合同内容哈希值" prop="contentHash">
          <el-input v-model="form.contentHash" placeholder="请输入合同内容哈希值" />
        </el-form-item>
        <el-form-item label="到验号" prop="uniqueCode">
          <el-input v-model="form.uniqueCode" placeholder="请输入到验号" />
        </el-form-item>
        <el-form-item label="船名" prop="shipName">
          <el-input v-model="form.shipName" placeholder="请输入船名" />
        </el-form-item>
        <el-form-item label="作废日期" prop="abolishDate">
          <el-date-picker clearable
                          v-model="form.abolishDate"
                          type="date"
                          value-format="yyyy-MM-dd"
                          placeholder="请选择作废日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="公司ID" prop="companyId">
          <el-input v-model="form.companyId" placeholder="请输入公司ID" />
        </el-form-item>
        <el-form-item label="合同总量" prop="volume">
          <el-input v-model="form.volume" placeholder="请输入合同总量" />
        </el-form-item>
        <el-form-item label="单位" prop="unit">
          <el-input v-model="form.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item label="单价" prop="price">
          <el-input v-model="form.price" placeholder="请输入单价" />
        </el-form-item>
        <el-form-item label="币种" prop="currency">
          <el-input v-model="form.currency" placeholder="请输入币种" />
        </el-form-item>
        <el-form-item label="备注说明" prop="remarks">
          <el-input v-model="form.remarks" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitReview">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listcontractmanadge, getcontractmanadge, delcontractmanadge, addcontractmanadge, updatecontractmanadge } from "@/api/BussinessContract/contractmanadge";
import { resource } from '@/plugins/download/'
import { listAudition } from "@/api/audition/audition";
export default {
  name: "合同管理",
  dicts: ['sys_contract_cstatus', 'sys_contract_type','sys_port_owner','sys_port_owner2'],
  data() {
    return {
      currentStatus: '草案',  // 默认状态
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // BussinessContract表格数据
      contractmanadgeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      openUpdate: false,
      openReview: false, // 送审对话框显示状态
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        port: null,
        owner: null,
        contractType: null,
        signDate: null,
        effectDate: null,
        expirationDate: null,
        status: '草案',
        contentSummary: null,
        contentUrl: null,
        contentHash: null,
        uniqueCode: null,
        shipName: null,
        abolishDate: null,
        companyId: null,
        cargoType: null,
        volume: null,
        unit: null,
        price: null,
        currency: null,
        remarks: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      },
      // 审核记录相关
      selectedContractId: null, // 当前选中的合同ID
      auditList: [], // 审核记录列表
      auditLoading: false, // 审核记录加载状态
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询BussinessContract列表 */
    getList() {
      this.loading = true;
      listcontractmanadge(this.queryParams).then(response => {
        this.contractmanadgeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openUpdate =false,
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        contractId: null,
        port: null,
        owner: null,
        contractType: null,
        signDate: null,
        effectDate: null,
        expirationDate: null,
        status: null,
        contentSummary: null,
        contentUrl: null,
        contentHash: null,
        uniqueCode: null,
        shipName: null,
        abolishDate: null,
        companyId: null,
        cargoType: null,
        volume: null,
        unit: null,
        price: null,
        currency: null,
        remarks: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.contractId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加合同";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const contractId = row.contractId || this.ids
      getcontractmanadge(contractId).then(response => {
        this.form = response.data;
        this.openUpdate = true;
        this.title = "修改合同";
      });
    },
    /** 送审按钮操作 */
    handleSubmitReview() {
      if (this.ids.length === 0) {
        this.$message.warning('请先选择要送审的合同');
        return;
      }
      // 目前只支持单条送审
      if (this.ids.length > 1) {
        this.$message.warning('目前只支持单条合同送审');
        return;
      }
      const contractId = this.ids[0];
      getcontractmanadge(contractId).then(response => {
        this.form = response.data;
        this.openReview = true;
      });
    },
    /** 提交送审 */
    submitReview() {
      // 更新合同状态为"审核中"
      const submitData = {
        ...this.form,
        status: "审核中", // 假设"审核中"的状态值为审核中
      };
      updatecontractmanadge(submitData).then(response => {
        this.$modal.msgSuccess("合同送审成功");
        this.openReview = false;
        this.getList(); // 刷新列表
      }).catch(error => {
        this.$modal.msgError("合同送审失败");
        console.error(error);
      });
    },

    handleDownload(row){
      // var name = row.fileName;
      var url = row.contentUrl;
      this.$download.resource(url,false)
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null) {
            addcontractmanadge(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 提交按钮 */
    submitFormUpdate() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.contractId != null) {
            updatecontractmanadge(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
      handleTabClick(tab) {
        // 根据所选状态过滤合同列表
        if (this.currentStatus === '草案') {
          this.queryParams.status = '草案';  // 修改查询参数，过滤草案状态的合同
        } else if (this.currentStatus === '审核中') {
          this.queryParams.status = '审核中';  // 修改查询参数，过滤审核中的合同
        } else if (this.currentStatus === '审核退回') {
          this.queryParams.status = '审核退回';  // 修改查询参数，过滤审核退回的合同
        } else  {
          this.queryParams.status = '审核通过';  // 修改查询参数，过滤审核通过的合同
        }
        // 重新执行查询
        this.getList();  // 调用查询方法，获取新的数据
      },

    /** 删除按钮操作 */
    handleDelete(row) {
      const contractIds = row.contractId || this.ids;
      this.$modal.confirm('是否确认删除合同编号为"' + contractIds + '"的数据项？').then(function() {
        return delcontractmanadge(contractIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('BussinessContract/contractmanadge/export', {
        ...this.queryParams
      }, `合同管理_${new Date().getTime()}.xlsx`)
    },
    /** 行点击事件 */
    handleRowClick(row) {
      this.selectedContractId = row.contractId;
      this.getAuditList(this.selectedContractId);
    },

    /** 查询审核记录列表 */
    getAuditList(contractId) {
      this.auditLoading = true;
      listAudition({ contractId }).then(response => {
        this.auditList = response.rows;
        this.auditLoading = false;
      });
    },
  }
};
</script>
